package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.INewsInfoDAO;
import org.jsola.hr.dto.NewsInfoAddDTO;
import org.jsola.hr.dto.NewsInfoUpdateDTO;
import org.jsola.hr.entity.NewsInfoDO;
import org.jsola.hr.query.NewsInfoQuery;
import org.jsola.hr.service.INewsInfoService;
import org.jsola.hr.vo.NewsInfoListVO;
import org.jsola.hr.vo.NewsInfoVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 官网信息的类型
 *
 * @author zhr
 */
@Service("hrNewsInfoServiceImpl")
public class NewsInfoServiceImpl implements INewsInfoService {

    @Autowired
    private INewsInfoDAO newsInfoDAO;

    @Autowired
    private INewsInfoService newsInfoService;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public NewsInfoVO save(NewsInfoAddDTO newsInfoAddDTO, TokenUser tokenUser) {
        // 转DO
        NewsInfoDO newsInfoDO = newsInfoAddDTO.to(NewsInfoDO.class);
        // 保存
        newsInfoDO = save(newsInfoDO, "default", tokenUser.getUserId());
        return newsInfoDO.to(NewsInfoVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(NewsInfoUpdateDTO newsInfoUpdateDTO, TokenUser tokenUser) {
        //转DO
        NewsInfoDO newsInfoDO = newsInfoUpdateDTO.to(NewsInfoDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(newsInfoDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... newsInfoIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) newsInfoIds);
    }


    @Override
    public NewsInfoVO selectById(String newsInfoId, String siteId) {
        NewsInfoDO newsInfoDO = selectDOById(newsInfoId, siteId);
        if (newsInfoDO == null) {
            return null;
        }
        return newsInfoDO.to(NewsInfoVO.class);
    }

    @Override
    public List<NewsInfoListVO> select(NewsInfoQuery newsInfoQuery, String siteId) {
        List<NewsInfoDO> newsInfoDOList = selectDO(newsInfoQuery, siteId);
        if(CollectionUtils.isEmpty(newsInfoDOList)) {
            return newsInfoDOList == null ? null : new ArrayList<>();
        }
        return newsInfoDOList.stream()
                .map(newsInfoDO -> newsInfoDO.to(NewsInfoListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(NewsInfoQuery newsInfoQuery, String siteId) {
        Example example = buildExample(newsInfoQuery,siteId);
        return newsInfoDAO.selectCountByExample(example);
    }

    @Override
    public Page<NewsInfoListVO> selectPage(NewsInfoQuery newsInfoQuery, String siteId) {
        Example example = buildExample(newsInfoQuery,siteId);
        Page<NewsInfoDO> page = newsInfoDAO.selectPageByExample(example,
                newsInfoQuery.getPageNo(),
                newsInfoQuery.getPageSize());

        return page.to(NewsInfoListVO.class);
    }

    @Override
    public NewsInfoDO selectDOById(String newsInfoId, String siteId) {
        return listById(newsInfoId, "default");
    }

    @Override
    public List<NewsInfoDO> selectDO(NewsInfoQuery newsInfoQuery, String siteId) {
        Example example = buildExample(newsInfoQuery,siteId);
        return newsInfoDAO.selectByExample(example);
    }

    @Override
    public NewsInfoListVO selectNewsInfoByNewsTypeId(String newsTypeId) {
        //查询到新闻信息
        NewsInfoListVO newsInfoListVO = newsInfoDAO.selectInfoByNewsTypeId(newsTypeId);
        if(ObjectUtils.isEmpty(newsInfoListVO)){
            throw new ParamException("newsTypeId不存在");
        }
        //查询DO
        Example example = new Example(NewsInfoDO.class);
        example.and().andEqualTo("id",newsInfoListVO.getId());
        NewsInfoDO newsInfoDO = newsInfoDAO.selectOneByExample(example);
        //赋值浏览量+1
        newsInfoDO.setBrowseNum(newsInfoDO.getBrowseNum() == null ? 1 : newsInfoDO.getBrowseNum()+1);
        newsInfoListVO.setBrowseNum(newsInfoDO.getBrowseNum());
        //更新,数据库,浏览量
        newsInfoDAO.updateByExampleSelective(newsInfoDO,example);
        return newsInfoListVO;
    }


    /**
     * 根据查询参数，构建example

     * @param newsInfoQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(NewsInfoQuery newsInfoQuery, String siteId) {
        Example example = new Example(NewsInfoDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", "default");
        if (newsInfoQuery.getNewsTypeId() != null) {
            example.and().andEqualTo("newsTypeId", newsInfoQuery.getNewsTypeId());
        }
        if (newsInfoQuery.getTitle() != null) {
            example.and().andEqualTo("title", newsInfoQuery.getTitle());
        }
        if (newsInfoQuery.getContent() != null) {
            example.and().andEqualTo("content", newsInfoQuery.getContent());
        }
        if (newsInfoQuery.getContentHasStyle() != null) {
            example.and().andEqualTo("contentHasStyle", newsInfoQuery.getContentHasStyle());
        }
        if (newsInfoQuery.getToUrl() != null) {
            example.and().andEqualTo("toUrl", newsInfoQuery.getToUrl());
        }
        if (newsInfoQuery.getIsPush() != null) {
            example.and().andEqualTo("isPush", newsInfoQuery.getIsPush());
        }
        if (newsInfoQuery.getPushDateStart() != null &&
                newsInfoQuery.getPushDateEnd() != null) {
            example.and().andBetween("pushDate", newsInfoQuery.getPushDateStart(),
                    newsInfoQuery.getPushDateEnd());
        }
        if (newsInfoQuery.getPushUserId() != null) {
            example.and().andEqualTo("pushUserId", newsInfoQuery.getPushUserId());
        }
        if (newsInfoQuery.getPushUserName() != null) {
            example.and().andEqualTo("pushUserName", newsInfoQuery.getPushUserName());
        }
        // 排序
        ExampleKit.setExampleOrder(example,newsInfoQuery.getOrders());
        return example;
    }
}




